<!--
	作者：2575202388@qq.com
	时间：2018-10-14
	描述：基础设置顶部
-->

<template>
	<div>
		<div style="padding-top:10px;width:100%;">
			<Form :label-width="120" label-position="right">
				<template v-for="(item,index) of params">
					<div style="width:100%;float:left;">
						<div style="float:left;">
							<FormItem  label="积分范围:">
							    <Input v-model="item.minjf" placeholder="请输入最小积分" clearable style="width: 150px;float: left;"></Input>
							    	<label style="width: 10px;float: left;text-align: center;">-</label>
							    <Input v-model="item.maxjf" placeholder="请输入最大积分" clearable style="width: 150px;float: left;"></Input>
							</FormItem>
						</div>
						<div style="float:left;">
							<FormItem  label="对应称号:">
								<Input v-model="item.ch" placeholder="请输入对应称号" clearable style="width: 150px;float: left;"></Input>
							</FormItem>	
						</div>
						<div style="float:left;">
							<FormItem>
								<Icon type="md-remove-circle" style="color: #8B2500;font-size: 20px;"  v-on:click="del(index)"/>
							</FormItem>	
						</div>
			      	</div>
				</template>
			</Form>	
		</div>
		<div style="width:100%;height: 60px;line-height: 60px;float:left;margin-bottom: 20px;">
			<Button type="info" style="float:left;margin-left:10%;" v-on:click="save()">保存</Button>
			<Button v-on:click="addCh()" icon="md-add" type="primary" style="background-color: #458B74;border:1px solid #458B74;float: left; margin-left: 10%;">添加对应积分与称号</Button>
		</div>	
	</div>
</template>
<script>
	
	export default {
		name:"jfych",
		
		data() {
			
			return {	
				params:[
					{
						minjf:'',
						maxjf:'',
						ch:''
					}
				]
			}
		},
		components: {
			
		},
		computed: {
			
		},
		methods: {
			addCh(){
				var params=this.params;
				var par={
					minjf:'',
					maxjf:'',
					ch:''
				};
				params.push(par);
				this.params=params;
			}, 
			
			del(e){
				var params=this.params;
				params.splice(e, 1);
				this.params=params;
			}, 
			
			
			getjfych(){
				//获取积分与称号
				this.$http.get("/host/readbook/jfych/getJfych.html")
				.then(result => {
					this.params=result;
				});
			}, 
			
			save(){
				var params=this.params;
				for(var i=0;i<params.length;i++){
					var par=params[i];
					var minjf=par.minjf;
					var maxjf=par.maxjf;
					var ch=par.ch;
					
					let reg = /^\+?[1-9][0-9]*$/; 
					if(!reg.test(minjf)){
						this.$Modal.error({
                            title: "提示",
                            content: "最小积分只能为正整数！"
                        });	
                        return;
					}
					
					if(i!=(params.length-1)){
						if(!reg.test(maxjf)){
							this.$Modal.error({
	                            title: "提示",
	                            content: "第"+(i+1)+"行最大积分只能为正整数且不能为空！"
	                        });	
	                        return;
						}
					}else{
						if(maxjf){
							if(!reg.test(maxjf)){
								this.$Modal.error({
		                            title: "提示",
		                            content: "最后一行最大积分只能为正整数！"
		                        });	
		                        return;
							}
						}
					}

					
					//除去最后一行 其他的最大积分均不能为空
					if(i!=(params.length-1)){
						if(!maxjf){
							this.$Modal.error({
	                            title: "提示",
	                            content: "除去最后一行,最大见积分不能为空！"
	                        });	
	                        return;
						}
					}
					
					
					if(!ch){
						this.$Modal.error({
                            title: "提示",
                            content: "请输入最小称号！"
                        });	
                        return;
					}
					
					//检查最小积分是否大于上一个序列的最大积分
					if(i>0){
						var smaxjf=params[(i-1)].maxjf;
						if(minjf<smaxjf){
							this.$Modal.error({
	                            title: "提示",
	                            content: "第"+(i+1)+"行最小积分应大于第"+(i)+"行最大积分！"
	                        });	
	                        return;
						}
					}
							
				}

				//保存积分与称号
				this.$http.post("/host/readbook/jfych/saveJfych.html",{params:JSON.stringify(params)})
				.then(result => {
					if(result){
						this.$Modal.success({
                            title: "提示",
                            content: "保存成功！"
                      	});
					}else{
						this.$Modal.error({
                            title: "提示",
                            content: "添加失败，请联系管理员！"
                        });	
					}
				});
			}, 
			
		}, 
		mounted(){	
			//获取积分与称号
			this.getjfych();
		},
		watch:{
			
		},
	}
</script>

<style>
</style>



